/***************************************************************************
 * Copyright (C) 2005 Global Biodiversity Information Facility Secretariat.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 ***************************************************************************/
package org.gbif.agi.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * The remote id stores the identifier that the datasource knows
 * the genus_usage as 
 * @author trobertson
 */
@Entity
@Table(name="remote_id")
public class RemoteId implements Comparable<Object> {
	private static final long serialVersionUID = -2602850579630564882L;
	
	private Long id;
	private GenusUsage genusUsage;
	private String remoteId;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(columnDefinition="int")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="remote_id", nullable=false)
	public String getRemoteId() {
		return remoteId;
	}
	public void setRemoteId(String remoteId) {
		this.remoteId = remoteId;
	}
	
	@OneToOne (optional=false)
	@JoinColumn (name="usage_id")
	public GenusUsage getUsage() {
		return genusUsage;
	}
	public void setUsage(GenusUsage genusUsage) {
		this.genusUsage = genusUsage;
	}
	
	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		RemoteId myClass = (RemoteId) object;
		return new CompareToBuilder().append(this.remoteId, myClass.remoteId)
				.append(this.genusUsage, myClass.genusUsage).append(
						this.id, myClass.id).toComparison();
	}
	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof RemoteId)) {
			return false;
		}
		RemoteId rhs = (RemoteId) object;
		return new EqualsBuilder().append(this.remoteId, rhs.remoteId).append(
				this.genusUsage, rhs.genusUsage)
				.append(this.id, rhs.id).isEquals();
	}
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(1568975905, -1289325633).append(
				this.remoteId).append(this.genusUsage).append(this.id)
				.toHashCode();
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("id", this.id)
			.append("genusUsage", this.genusUsage).append(
				"remoteId", this.remoteId).toString();
	}
}
